package io.github.talelin.latticy.common.configuration;

import io.github.talelin.autoconfigure.exception.NotFoundException;
import lombok.extern.slf4j.Slf4j;
import org.kie.api.KieBase;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.FactHandle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @authoer:wulongbo
 * @createDate:2022/8/8
 * @description:
 */
@Slf4j
public class MyKieContext {
    private KieSession kieSession;

    private KieBase kieBase;

    private KieContainer kieContainer;

    private List<FactHandle> factHandleRecord = new ArrayList<>();

    public MyKieContext(KieSession kieSession, KieBase kieBase, KieContainer kieContainer) {
        this.kieSession = kieSession;
        this.kieBase = kieBase;
        this.kieContainer = kieContainer;
    }

    public MyKieContext(KieContainer kieContainer) {
        if (kieContainer != null) {
            this.kieContainer = kieContainer;
            this.kieSession = kieContainer.newKieSession();
            this.kieBase = kieContainer.getKieBase();
        }
    }

    public KieSession getKieSession() {
        return kieSession;
    }

    private void setKieSession(KieSession kieSession) {
        this.kieSession = kieSession;
    }

    public KieBase getKieBase() {
        return kieBase;
    }

    private void setKieBase(KieBase kieBase) {
        this.kieBase = kieBase;
    }

    public KieContainer getKieContainer() {
        return kieContainer;
    }

    private void setKieContainer(KieContainer kieContainer) {
        this.kieContainer = kieContainer;
    }

    public FactHandle insert(Object o) {
        FactHandle insert = this.kieSession.insert(o);
        factHandleRecord.add(insert);
        return insert;
    }

    public void delete(FactHandle factHandle) {
        try {
            this.kieSession.delete(factHandle);
            factHandleRecord.remove(factHandle);
        } catch (Exception e) {
            log.error("移除FactHandle时出现异常,异常为:{}", e.getMessage());
        }
    }

    public void clean() {
        if (this.kieSession != null) {
            factHandleRecord.forEach(f -> {
                try {
                    this.kieSession.delete(f);
                } catch (Exception e) {
                    log.error("移除FactHandle时出现异常,异常为:{}", e.getMessage());
                }
            });
            factHandleRecord.clear();
        }
    }

    /**
     * 匹配规则
     *
     * @return
     */
    public int fireAllRules() {
        return this.kieSession.fireAllRules();
    }

    /**
     * 更新规则
     *
     * @param kieSession
     * @param kieBase
     * @param kieContainer
     */
    public void update(KieSession kieSession, KieBase kieBase, KieContainer kieContainer) {
        dispose();
        this.kieSession = kieSession;
        this.kieBase = kieBase;
        this.kieContainer = kieContainer;
    }

    /**
     * 更新规则
     *
     * @param kieContainer
     */
    public void update(KieContainer kieContainer) {
        if (kieContainer == null) {
            throw new NotFoundException(18710);
        }
        update(kieContainer.newKieSession(), kieContainer.getKieBase(), kieContainer);
    }

    /**
     * 清除规则
     */
    public void dispose() {
        if (this.kieSession != null) {
            this.clean();
            this.kieSession.dispose();
        }
        if (this.kieContainer != null) {
            this.kieContainer.dispose();
        }
    }

    public boolean isNullDrools() {
        return kieBase == null;
    }

    /**
     * 获取当前规则中所有的包和方法
     *
     * @return key 包名,value 方法名
     */
    public Map<String, List<String>> droolsBaseInfo() {
        Map<String, List<String>> map = new HashMap<>();
        if (isNullDrools()) {
            return map;
        }
        this.kieBase.getKiePackages().forEach(c -> {
            String packageName = c.getName();
            List<String> list = new ArrayList<>();
            c.getRules().forEach(r -> list.add(r.getName()));
            if (!list.isEmpty()) {
                map.put(packageName, list);
            }
        });
        return map;
    }

}
